package work.vcloud.baseinfo.controller;

import work.vcloud.frame.controller.BaseController;
import work.vcloud.frame.dao.OpenDao;
import work.vcloud.frame.vo.OutputVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

@RestController
@Scope("prototype")
public class CenterController extends BaseController {

	@Autowired
	OpenDao openDao;

	/**
	 * 添加主管单位
	 */
	@RequestMapping("api/unit.insertCenter")
	public OutputVo insertCenter() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("sqlMapId", "unit.selectCenterExit");
			Integer result = (Integer)openDao.queryForObject(map);
			if(result>0) {
				return output("9999","主管单位名称已存在","");
			}else {
				//插入单位
				map.put("type", "0");
				map.put("sqlMapId", "unit.insertUnit");
				map.put("fk_unit", openDao.insert(map));
	
				//插入主管单位
				map.put("sqlMapId", "unit.insertCenter");
				openDao.insert(map);
				return output("0000");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	
	/**
	 * 修改主管单位
	 */
	@RequestMapping("api/unit.updateCenter")
	public OutputVo updateCenter() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("sqlMapId", "unit.selectCenterExit");
			Integer result = (Integer)openDao.queryForObject(map);
			if(result>0) {
				return output("9999","主管单位名称已存在","");
			}else {
				//修改单位
				map.put("sqlMapId", "unit.updateUnit");
				openDao.update(map);
	
				//修改主管单位
				map.put("sqlMapId", "unit.updateCenter");
				openDao.update(map);
				return output("0000");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	
	/**
	 * 删除主管单位
	 */
	@RequestMapping("api/unit.deleteCenter")
	public OutputVo deleteCenter() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			//修改主管单位
			map.put("sqlMapId", "unit.deleteCenter");
			openDao.update(map);

			//修改单位
			map.put("sqlMapId", "unit.deleteUnit");
			openDao.update(map);
			
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	
	/**
	 * 清理主管单位
	 */
	@RequestMapping("api/unit.clearCenter")
	public OutputVo clearCenter() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			//修改主管单位
			map.put("sqlMapId", "unit.clearCenter");
			openDao.delete(map);

			//修改单位
			map.put("sqlMapId", "unit.clearUnit");
			openDao.delete(map);
			
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
}
